import registnet.dao.Student;
import registnet.dao.StudentPeer;

import registnet.dao.SystemLogPeer;
import registnet.dao.Grade;		
import registnet.dao.GradePeer;
import registnet.dao.CoursePeer;
		
import resources.Constants;

GradePeer gPeer = new GradePeer();
registnet.dao.SystemPropertiesPeer spp = new registnet.dao.SystemPropertiesPeer();

/* [start] 	initializes gui variables */
String _gradesheet = bundle.getProperty("registnet.module.grades.gradesheet");

String _academic_year = bundle.getProperty("registnet.module.grades.macro.gradesheet.search.academic_year");
String _semester = bundle.getProperty("registnet.module.grades.macro.gradesheet.search.semester");
String _course = bundle.getProperty("registnet.module.grades.macro.gradesheet.search.course");
String _section = bundle.getProperty("registnet.module.grades.macro.gradesheet.search.section");

String _student_number = bundle.getProperty("registnet.module.grades.macro.gradesheet.form.student_number");
String _full_name = bundle.getProperty("registnet.module.grades.macro.gradesheet.form.full_name");
String _sex = bundle.getProperty("registnet.module.grades.macro.gradesheet.form.sex");
String _college = bundle.getProperty("registnet.module.grades.macro.gradesheet.form.college");
String _degree_course = bundle.getProperty("registnet.module.grades.macro.gradesheet.form.degree_course");
String _grade = bundle.getProperty("registnet.module.grades.macro.gradesheet.form.grade");

List courses = new CoursePeer().getCoursesByUnit(new UnitPeer().getUnitId(_office));

HashMap gradeMap = new HashMap();
List grades;
/* [end] */		

/* setting the user for logs */
gPeer.setUser(_user, _office);
/* [end] */


/* [start]	defines a renderer for the grades */
private class GradeSubmissionRenderer implements RowRenderer  {
	public void render(Row row, java.lang.Object data) {
		new Label(data[0]).setParent(row);
		new Label(data[1]).setParent(row);
		new Label(data[2]).setParent(row);
		Combobox cb=new Combobox();
		cb.setReadonly(true);
		cb.setStyle("background: #ffffff");
		cb.setId(data[1]);
		String[] grade_constants = Constants.GRADE;
		for(int i=0; i<grade_constants.length; i++){
			Comboitem ci = new Comboitem(grade_constants[i]);
			ci.setParent(cb);
		}
		cb.setParent(row);
		cb.setWidth("50px");
		Textbox tb = new Textbox();
		tb.setId(data[1]+"_r");
		tb.setCols(15);
		tb.setRows(2);
		tb.setParent(row);
	}
}
/* [end] */




/*void retrieveGrades(){
	String courseValue = course_code.value;
	String sectionValue = section.value;
	int semester = spp.getSystemProperties().getSemester();
	int year = spp.getSystemProperties().getAcademicYear();
	if(semester==3){
		year++;
	}
	Integer courseOfferingId = new CourseOfferingPeer().getCourseOfferingId(new CoursePeer().getCourseId(courseValue), sectionValue, semester, year);
	if(courseOfferingId==null){
		alert("Section not found.");
		return;
	}
	
	List classlist = new SlotPeer().getStudentsByCourseOffering(courseOfferingId);
	ListIterator iter = classlist.listIterator();
	String[][] listModel = new String[classlist.size()][6];
	int i = 0;
	while(iter.hasNext()){
		Slot slot = iter.next();
		Student student = new StudentPeer().getStudent(slot.getStudentId());
		listModel[i][0] = "" + (i + 1);
		listModel[i][1] = student.getStudentNumber();
		listModel[i][2] = student.getLastName() + ", " + student.getFirstName() + " " + student.getMiddleName();
		listModel[i][3] = student.getSex();
		registnet.dao.DegreeCourse dp = new registnet.dao.DegreeCoursePeer().getDegreeCourse(student.getCurriculum().getDegreeId());
		listModel[i][4] = new registnet.dao.CollegePeer().getCollege(dp.getCollegeId()).getCollegeCode();
		listModel[i][5] = dp.getDegreeCode();
		i++;
	}
	ListModel strset = new SimpleListModel(listModel);
	contents_win.getFellow("gradesub_g").setModel(strset);
	contents_win.getFellow("gradesub_g").setRowRenderer(new GradeSubmissionRenderer());
	
}*/

/* [start] 	retrieves the grades and places them on the listbox */
void retrieveGrades(){
	Integer courseValue = new CoursePeer().getCourseId(course_code.value);
	String sectionValue = section.value;
	int semester = spp.getSystemProperties().getSemester();
	int year = spp.getSystemProperties().getAcademicYear();
	if(semester==3){
		year++;
	}
	grades = gPeer.getGradeBySection(courseValue, sectionValue, semester, year);
	
	String[][] listModel = new String[grades.size()][5];
	for(int i=0; i<grades.size(); i++){
		Grade grade = grades.get(i);
		Student student = new StudentPeer().getStudent(grade.getStudentId());
		listModel[i][0] = "" + (i + 1);
		listModel[i][1] = student.getStudentNumber();
		listModel[i][2] = student.getLastName() + ", " + student.getFirstName() + " " + student.getMiddleName();
		if(!Constants.MAP_GRADE_VALUE.get(grade.getGrade()).equals("REGD")){
			alert("Grades are already submitted for this section.");
			return;
		}
	}
	ListModel strset = new SimpleListModel(listModel);
	contents_win.getFellow("gradesub_g").setModel(strset);
	contents_win.getFellow("gradesub_g").setRowRenderer(new GradeSubmissionRenderer());
}
/* [end] */


/* [start]	defines a renderer for the grades */
public class SectionRenderer implements ListitemRenderer {
	public void render(Listitem item, java.lang.Object data) {
		new Listcell(data).setParent(item);
	}
}
/* [end] */

public class UIItemRenderer implements ListitemRenderer {
	public void render(Listitem item, java.lang.Object data) {
		new Listcell(data).setParent(item);
	}
}
void retrieveCourses(){											  
	String[] listModel = new String[courses.size()];
	for (int i=0; i<courses.size(); i++)
		listModel[i] = courses.get(i).getCourseCode();
	ListModel strset = new ListModelList(listModel);
	course_code_lb.setModel(strset);
	course_code_lb.setItemRenderer(new UIItemRenderer());
}

void filterCourse(String string){
	ListIterator i = courses.listIterator();
	List list = new java.util.Vector();
	while(i.hasNext()){
		String temp = i.next().getCourseCode(); 
		if(temp.toUpperCase().startsWith(string.toUpperCase())){
			list.add(temp);
		}
	}
	course_code_lb.getModel().clear();
	course_code_lb.getModel().addAll(list);
}

/* [start] 	retrieves the sections and places them on the listbox */
void retrieveSections(){
	String courseValue = course_code.value;
	int semester = spp.getSystemProperties().getSemester();
	int year = spp.getSystemProperties().getAcademicYear();
	if(semester==3){
		year++;
	}
	List sections = new CourseOfferingPeer().getCourseOfferingNames(new CoursePeer().getCourseId(courseValue), year, semester);
	ListModel strset = new ListModelList(sections);
	section_lb.setModel(strset);
	section_lb.setItemRenderer(new SectionRenderer());
}
/* [end] */

void submitGrades(){
	if(grades!=null){
		for(int i=0; i<grades.size(); i++){
			String studentNumber = new StudentPeer().getStudent(grades.get(i).getStudentId()).getStudentNumber();
			if(contents_win.getFellow(studentNumber).getSelectedItem()==null){
				alert("Check infomation supplied");
				return;
			}
			Double gradeValue = Constants.MAP_GRADE_KEY.get(contents_win.getFellow(studentNumber).getSelectedItem().getLabel());
			String remarks = contents_win.getFellow(studentNumber+"_r").getValue();
			grades.get(i).setGrade(gradeValue);
			grades.get(i).setRemarks(remarks);
		}
		
		for(int i=0; i<grades.size(); i++){
			gPeer.updateGrade(grades.get(i));
		}
		alert("Grade Submission successful.");
		section.setValue("");
		retrieveGrades();
	}
}

private class ClearRenderer implements RowRenderer  {
	public void render(Row row, java.lang.Object data) {
	}
}
